package com.pyg.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pyg.manager.service.SpecificationService;
import com.pyg.mapper.TbSpecificationMapper;
import com.pyg.mapper.TbSpecificationOptionMapper;
import com.pyg.pojo.TbSpecification;
import com.pyg.pojo.TbSpecificationOption;
import com.pyg.pojo.TbSpecificationOptionExample;
import com.pyg.utils.PageResult;
import com.pyg.utils.Specification;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * Created by on 2018/8/10.
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;
    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public PageResult findByPage(Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        Page<TbSpecification> specificationList = (Page<TbSpecification>) specificationMapper.selectByExample(null);
        PageResult pageResult = new PageResult(specificationList.getTotal(), specificationList.getResult());

        return pageResult;
    }

    /**
     * 需求：添加
     */
    public void add(Specification specification) {
        TbSpecification tbSpecification = specification.getSpecification();
        specificationMapper.insert(tbSpecification);//添加规格
        List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
        //遍历,添加规格
        for (TbSpecificationOption specificationOption : specificationOptionList) {
            specificationOption.setSpecId(tbSpecification.getId());
            specificationOptionMapper.insert(specificationOption);
        }
    }

    @Override
    public Specification findOne(Long id) {
        //查询规格,封装
        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
        Specification specification = new Specification();
        specification.setSpecification(tbSpecification);
        //根据外键查询规格属性
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        //设置想查询条件
        criteria.andSpecIdEqualTo(id);
        //执行查询
        List<TbSpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(example);
        //封装结果
        specification.setSpecificationOptionList(specificationOptions);
        return specification;
    }

    @Override
    public void update(Specification specification) {
        //更新规格名称
        TbSpecification tbSpecification = specification.getSpecification();
        specificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //删除规格对象
        Long id = tbSpecification.getId();
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(example);
        //遍历添加新的规格对象
        List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
        for (TbSpecificationOption tbSpecificationOption : specificationOptionList) {
            //设置外键
            tbSpecificationOption.setSpecId(id);
            specificationOptionMapper.insertSelective(tbSpecificationOption);
        }


    }

    @Override
    public void del(Long[] ids) {
        //删除规格
        for (Long id : ids) {
            specificationMapper.deleteByPrimaryKey(id);
            //删除所有的规格选项
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            specificationOptionMapper.deleteByExample(example);
        }
    }

    @Override
    public List<Map> findSpecificationList() {
        return specificationMapper.findSpecificationList();
    }
}
